package com.jzqf.compose

import android.annotation.SuppressLint
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.material3.Button
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Dialog
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import androidx.core.view.WindowCompat
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewmodel.compose.viewModel

/**
 * API:https://developer.android.google.cn/training/wearables/apps/splash-screen?hl=zh-cn
 * 2024/3/26 14:01
 * @author JiangFeng
 */
@SuppressLint("CustomSplashScreen")
class SplashActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        installSplashScreen()
        super.onCreate(savedInstanceState)
        WindowCompat.setDecorFitsSystemWindows(window, false)
        val controller = WindowCompat.getInsetsController(window, window.decorView)
        controller.isAppearanceLightStatusBars = true
        setContent {
            SplashScreen()
        }
    }

}

@Composable
fun SplashScreen() {
    var showPrivacyDialog by remember { mutableStateOf(true) } // 控制是否显示隐私协议Dialog
    if (showPrivacyDialog) {
        PrivacyDialog(onAgree = {
            // 当用户同意后，这里可以处理相应的逻辑，例如跳转至主页面
            showPrivacyDialog = false
        })
    } else {

    }
}


class PrivacyPolicyViewModel : ViewModel() {
    var isAgreed by mutableStateOf(false)

    fun agreeToPolicy() {
        isAgreed = true
        // 这里可以添加保存用户同意状态到持久化存储的逻辑
    }
}

@Composable
fun PrivacyDialog(onAgree: () -> Unit) {
    val context = LocalContext.current
    val viewModel: PrivacyPolicyViewModel = viewModel()

    Dialog(onDismissRequest = { /* 当点击外部区域关闭Dialog */ }) {
        Surface(
            modifier = Modifier
                .fillMaxWidth()
                .wrapContentHeight(),
            shape = MaterialTheme.shapes.medium,
            color = Color.White
        ) {
            Column(
                modifier = Modifier.padding(16.dp),
                verticalArrangement = Arrangement.Center,
                horizontalAlignment = Alignment.CenterHorizontally
            ) {
                Text(text = "隐私协议")
                Spacer(modifier = Modifier.height(16.dp))
                // 加载并显示隐私协议文本
                Text(text = "此处加载隐私协议具体内容...")
                Spacer(modifier = Modifier.height(16.dp))

                Row(
                    modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.End
                ) {
                    Button(
                        onClick = {
                            viewModel.agreeToPolicy()
                            onAgree() // 用户同意时执行的操作，比如关闭Dialog并继续启动流程
//                            snackbarHostState.showSnackbar("隐私协议已同意")
                        }, modifier = Modifier.padding(end = 16.dp)
                    ) {
                        Text("同意并继续")
                    }
                }
            }
        }
    }
}